package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by Administrator on 2017/4/17.
 * 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
 */
public class Multiply {
    public int[] multiply(int[] A) {
        if (A == null || A.length == 0) return null;
        int length = A.length;
        int sum = 1;
        int[] record = new int[length];
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < i; j++) {
               record[j] = record[j]*A[i];
            }
            record[i] = sum;
            sum*=A[i];
        }
        return record;
    }

    public static void main(String[] args) {
        Multiply multiply = new Multiply();

        int[] multiply1 = multiply.multiply(new int[]{1, 2, 3, 4, 5});
        for (int i = 0; i < multiply1.length; i++) {
            System.out.print(multiply1[i] + "\t");
        }
    }
}
